package app.fugema.shop.main.order.view;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.LogUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewObserver;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import app.fugema.shop.R;
import app.fugema.shop.databinding.ActivityAfterSaleSelectBinding;
import app.fugema.shop.main.order.adapter.AfterSaleSelectAdapter;
import app.fugema.shop.main.order.model.OrderDetailResult;
import app.fugema.shop.main.order.model.OrderItem;
import app.fugema.shop.main.order.vm.OrderListVM;

public class OrderAfterSaleSelectActivity extends BaseTitleActivity<ActivityAfterSaleSelectBinding, OrderListVM> {
    //申请退款 类型选择页面

    public static void start(Context context,String orderId,String itemId)
    {
        Intent intent = new Intent(context, OrderAfterSaleSelectActivity.class);
        intent.putExtra("orderNumber", orderId);
        intent.putExtra("itemId", UIUtils.isEmpty(itemId) ? 0:Integer.parseInt( itemId ));
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_after_sale_select);
    }

    private OrderDetailResult detailResult;
    private boolean isOnlyMoney;
    private long orderId;
    private String userKey;
    private AfterSaleSelectAdapter orderDetailAdapter;
    private int itemId;

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("申请售后",R.color.white);

        //选择退款退货
        binding.rlBtnRefundGoods.setOnClickListener(new View.OnClickListener()
        {
                @Override
                public void onClick(View v)
                {
                    OrderAfterSaleCreateActivity.start(OrderAfterSaleSelectActivity.this , String.valueOf(orderId) ,String.valueOf(itemId));
                    finish();
                }
        });
        //退款
        binding.rlBtnRefundMoney.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                OrderAfterSaleCreateActivity.start(OrderAfterSaleSelectActivity.this, String.valueOf(orderId),"");
                finish();
            }
        });

        orderDetailAdapter = new AfterSaleSelectAdapter(this, viewModel);
        binding.rvRecyclerViewGoods.setAdapter(orderDetailAdapter);

    }

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        userKey = LoginUtils.getToken();
        String orderNumber = intent.getStringExtra("orderNumber");
        if( UIUtils.isNotEmpty( orderNumber ) )
        {
            orderId = Long.parseLong( orderNumber );
        }
        itemId = intent.getIntExtra("itemId",0);
        LogUtil.e("接受的 itemId = "+itemId);
       viewModel.getOrderDetail(orderId,userKey).observe(this, new BaseViewObserver<LiveDataWrapper<OrderDetailResult>>(getActivity())
       {
            @Override
            protected void onSuccess(LiveDataWrapper<OrderDetailResult> data)
            {
                detailResult = data.data;

                if( detailResult.getOrder().getOrderStatus() == 2 )
                {
                    isOnlyMoney = true;
                }
                else
                {
                    isOnlyMoney = false;
                }
                if( isOnlyMoney )
                {
                    binding.rlBtnRefundGoods.setVisibility(View.GONE);
                    binding.rlBtnRefundMoney.setVisibility(View.VISIBLE);
                }
                else
                {
                    binding.rlBtnRefundGoods.setVisibility(View.VISIBLE);
                    binding.rlBtnRefundMoney.setVisibility(View.GONE);
                }
                binding.tvShopNameTitle.setText( detailResult.getOrder().getShopName() );

                if( itemId != 0  )
                {
                    List<OrderItem> tempList = new ArrayList<>();
                    for (OrderItem orderItem : detailResult.getOrderItem())
                    {
                        if( orderItem.getItemId() == itemId )
                        {
                            tempList.add( orderItem );
                        }
                    }
                    orderDetailAdapter.setDataList( tempList );
                }
                else
                {
                    orderDetailAdapter.setDataList( detailResult.getOrderItem() );
                }
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<OrderDetailResult> data)
            {
                return UIUtils.isEmpty(data.data.getOrder());
            }
        });
    }

}
